Synchronization Hardware
test_and_set Instruction
Solution using test_and_set()
compare_and_swap Instruction
程式說明:
int compare_and_swap(int * value, int expected, int new_value) {
int temp = *value;
if (*value == expected)
*value = new_value;
return temp;
}
==>總共會有三個value,分別是「original value(原來值)」、「expected value(期待值)」、「new_value」。
==>return原來值出去,數值進入後判斷variable的value有無跟期待值一樣,如果一樣的話,就將new_value設成value,然後將原來的value return會來,將內容作swap的動作。
Mutex Lock - Spinlock
Semaphore
Semaphore Usage
Semaphore Implementation
Semaphore Implementation with no Busy waiting
Deadlock and Starvation